package service;

import com.sun.net.httpserver.Authenticator;
import common.Pagination;
import common.StringUtils;
import dao.CroBuycarsDao;
import dao.CroPurchaseDao;
import dao.CroReturngoodsDao;
import entity.CroBuycars;
import entity.CroCustomer;
import entity.CroPurchase;
import entity.CroReturngoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroReturngoodsVo;

import java.util.List;

/**
 * Created by wsx on 2017/5/11.
 */
@Service
public class CroReturngoodsService {
    @Autowired
    private CroReturngoodsDao croReturngoodsDao;

    /**
     * 前台添加退货申请
     * @param goods
     * @return
     */
    @Transactional(readOnly = false)
    public boolean addReturnGoods(CroReturngoods goods){
        try {
            croReturngoodsDao.save(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /*
    * 分页获取所有的退货申请
    * */
    @Transactional(readOnly = true)
    public Pagination<CroReturngoods> findAllReturngoods(int index, int size){
        String hql="from CroReturngoods where del_flag='0' and returntype='0'";
        return croReturngoodsDao.findPagination(hql,index,size);
    }

     /*
    * 分页查找获取所有的退货申请
    * */
    @Transactional(readOnly = true)
    public Pagination<CroReturngoods> findReturngoodsByConditction(int index, int size, CroReturngoodsVo croReturngoodsVo){
       StringBuffer sb=new StringBuffer();
       sb.append("from CroReturngoods where del_flag='0' and returntype='0'");
        if(StringUtils.isNotBlank(croReturngoodsVo.getReturnstatus())&&!(croReturngoodsVo.getReturnstatus().equals("-1"))){
            sb.append(" and returnstatus='"+croReturngoodsVo.getReturnstatus()+"'");
        }
        if(StringUtils.isNotBlank(croReturngoodsVo.getUsername())){
            sb.append(" and croPurchase.croCustomer.username like'%"+croReturngoodsVo.getUsername()+"%'");
        }
        if(StringUtils.isNotBlank(croReturngoodsVo.getPurchaseid())){
            sb.append(" and croPurchase.id='"+croReturngoodsVo.getPurchaseid()+"'");
        }
        return croReturngoodsDao.findPagination(sb.toString(),index,size);
    }
    /*
      * 删除退货申请
      * */
    @Transactional(readOnly = false)
    public Boolean deleteReturngoods(String id){
        CroReturngoods croReturngoods=croReturngoodsDao.getById(id);
        croReturngoods.setDel_flag("1");
        croReturngoodsDao.update(croReturngoods);
        return true;

    }

    /*
    * 获取退货商品的详情
    * */
    @Transactional(readOnly = true)
    public CroReturngoods getReturngoodsDetail(String id){
//        Pagination<CroReturngoods> page;
       List<CroReturngoods> croReturngoodsList;
        StringBuffer sb=new StringBuffer();
        sb.append("from CroReturngoods where del_flag='0' and returntype='0' and id='"+id+"'");
//        page=croReturngoodsDao.findPagination(sb.toString(),1,1);
        croReturngoodsList=croReturngoodsDao.findByHqlExt(sb.toString(),CroReturngoods.class);
//        return page.getItems().get(0);
        return croReturngoodsList.get(0);
    }
    /*
* 改变退货状态
* */
    @Transactional(readOnly = false)
    public Boolean changeReturnGoodsStatus(String id,String status){
        if(StringUtils.isNotBlank(status)) {
            CroReturngoods croReturngoods = croReturngoodsDao.getById(id);
            croReturngoods.setReturnstatus(status);
        }
        return true;
    }
}
